package com.xx.job.seataorderservice2001.controller;

import com.xx.job.common.CommonResult;
import com.xx.job.entity.Account;
import com.xx.job.entity.Order;
import com.xx.job.entity.Storage;
import com.xx.job.seataorderservice2001.service.AccountService;
import com.xx.job.seataorderservice2001.service.OrderService;
import com.xx.job.seataorderservice2001.service.StorageSerivce;
import io.seata.spring.annotation.GlobalTransactional;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@Slf4j
@RestController
public class OrderController {

    @Autowired
    private OrderService orderService;
    @Autowired
    private AccountService accountService;
    @Autowired
    private StorageSerivce storageSerivce;

    /**
     * 加订单 减余额 减库存
     * @param order
     * @return
     */
    @Transactional
    @GlobalTransactional
    @RequestMapping("/order/create")
    public CommonResult create(Order order){
        // 加订单
        orderService.create(order);
        // 减余额
        Account account = new Account();
        account.setUserId(order.getUserId());
        account.setMoney(80);
        accountService.update(account);
        // 减库存
        Storage storage = new Storage();
        storage.setCount(999);
        storage.setCommodityCode(order.getCommodityCode());
        storageSerivce.update(storage);
        return new CommonResult(200,"添加订单成功！");
    }

}
